<template>
  <el-dialog
    v-model="dialogVisible"
    :title="mode === 'create' ? '新增员工' : '编辑员工'"
    width="600px"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" placeholder="请输入员工姓名" />
      </el-form-item>

      <el-form-item label="学历" prop="education">
        <el-select v-model="formData.education" placeholder="请选择学历">
          <el-option label="高中" value="高中" />
          <el-option label="大专" value="大专" />
          <el-option label="本科" value="本科" />
          <el-option label="硕士" value="硕士" />
          <el-option label="博士" value="博士" />
        </el-select>
      </el-form-item>

      <el-form-item label="工作年限" prop="workYears">
        <el-input-number
          v-model="formData.workYears"
          :min="0"
          :max="50"
          controls-position="right"
        />
      </el-form-item>

      <el-form-item label="部门" prop="department">
        <el-input v-model="formData.department" placeholder="请输入部门" />
      </el-form-item>

      <el-form-item label="岗位" prop="position">
        <el-input v-model="formData.position" placeholder="请输入岗位" />
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入邮箱" />
      </el-form-item>

      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入电话" />
      </el-form-item>

      <el-form-item label="入职日期" prop="joinDate">
        <el-date-picker
          v-model="formData.joinDate"
          type="date"
          placeholder="选择入职日期"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>

      <el-form-item label="简历" prop="resume">
        <el-input
          v-model="formData.resume"
          type="textarea"
          :rows="4"
          placeholder="请输入员工简历信息"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref, watch, nextTick } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'EmployeeDialog',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    employee: {
      type: Object,
      default: null
    },
    mode: {
      type: String,
      default: 'create'
    }
  },
  emits: ['update:modelValue', 'save', 'close'],
  setup(props, { emit }) {
    const dialogVisible = ref(false)
    const formRef = ref()
    const loading = ref(false)

    const formData = ref({
      name: '',
      education: '',
      workYears: 0,
      department: '',
      position: '',
      email: '',
      phone: '',
      joinDate: '',
      resume: ''
    })

    const rules = {
      name: [
        { required: true, message: '请输入员工姓名', trigger: 'blur' }
      ],
      education: [
        { required: true, message: '请选择学历', trigger: 'change' }
      ],
      department: [
        { required: true, message: '请输入部门', trigger: 'blur' }
      ],
      position: [
        { required: true, message: '请输入岗位', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ]
    }

    watch(
      () => props.modelValue,
      (newVal) => {
        dialogVisible.value = newVal
        if (newVal && props.employee) {
          // 编辑模式下填充数据
          nextTick(() => {
            formData.value = { ...props.employee }
          })
        } else if (newVal) {
          // 创建模式下重置数据
          formData.value = {
            name: '',
            education: '',
            workYears: 0,
            department: '',
            position: '',
            email: '',
            phone: '',
            joinDate: '',
            resume: ''
          }
        }
      }
    )

    watch(dialogVisible, (newVal) => {
      emit('update:modelValue', newVal)
    })

    const handleClose = () => {
      dialogVisible.value = false
      emit('close')
    }

    const handleSubmit = async () => {
      if (!formRef.value) return

      try {
        const valid = await formRef.value.validate()
        if (valid) {
          loading.value = true
          emit('save', { ...formData.value })
        }
      } catch (error) {
        ElMessage.error('请完善表单信息')
      } finally {
        loading.value = false
      }
    }

    return {
      dialogVisible,
      formRef,
      formData,
      rules,
      loading,
      handleClose,
      handleSubmit
    }
  }
}
</script>